﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace TVersityDotNet.HelperTypes
{
    /// <summary>
    /// Helper class representing a list of properties
    /// </summary>
    [DataContractAttribute]
    public class PropertyBag
    {
        [DataMember]
        private Dictionary<string, string> _propertyBag;

        /// <summary>
        /// Public constructor
        /// </summary>
        public PropertyBag()
        {
            this._propertyBag = new Dictionary<string, string>();
        }

        /// <summary>
        /// Method used to add properties to a propertybag safely
        /// </summary>
        /// <param name="propertyName">The property key</param>
        /// <param name="propertyValue">The property value</param>
        public void AddPropertyToPropertyBag(string propertyName, string propertyValue)
        {
            if (this.PropBag == null)
                this.PropBag = new Dictionary<string, string>();

            if (this.PropBag.ContainsKey(propertyName))
                this.PropBag[propertyName] = propertyValue;
            else
                this.PropBag.Add(propertyName, propertyValue);
        }

        /// <summary>
        /// Method used to safely return values from a property bag
        /// </summary>
        /// <param name="propertyName">The name of the property to get</param>
        /// <returns>The requested property</returns>
        public string GetPropertyFromPropertyBag(string propertyName)
        {
            if (this.PropBag == null)
                this.PropBag = new Dictionary<string, string>();

            if (this.PropBag.ContainsKey(propertyName))
                return this.PropBag[propertyName];
            else
                return null;
        }

        /// <summary>
        /// A dicrionary representing the list of properties with values
        /// </summary>
        [DataMember]
        public Dictionary<string, string> PropertiesWithValues
        {
            get
            {
                var returnDictionary = new Dictionary<string, string>();
                foreach (var propertyKey in this.PropBag.Keys)
                {
                    if (!string.IsNullOrEmpty(this.PropBag[propertyKey]))
                    {
                        returnDictionary.Add(propertyKey, this.PropBag[propertyKey]);
                    }
                }

                return returnDictionary;
            }
        }

        /// <summary>
        /// Property representing the property bag
        /// </summary>
        private Dictionary<string, string> PropBag
        {
            get { return this._propertyBag; }
            set { this._propertyBag = value; }
        }

        /// <summary>
        /// Returns wether or not a specified key is located in the property bag
        /// </summary>
        /// <param name="key">The key to search for</param>
        /// <param name="ignoreCase">Wether or not to ignore the case</param>
        /// <returns>True if the key exists, false if it doesn't</returns>
        public bool ContainsKey(string key, bool ignoreCase)
        {
            bool returnValue = false;
            foreach (string k in this.PropertiesWithValues.Keys)
            {
                if (string.Compare(key, k, ignoreCase) == 0)
                {
                    returnValue = true;
                    break;
                }
            }

            return returnValue;
        }

        /// <summary>
        /// Returns the number of properties with values
        /// </summary>
        /// <returns>Returns the number of properties with values</returns>
        [DataMember]
        public int Count
        {
            get
            {
                return this.PropertiesWithValues.Count;
            }
        }
    }
}
